// pages/shopList/shopList.js
import api from "../../utils/api.js"
// const common =require ("../../utils/common.wxs")
const app = getApp()
Page({
  data: {
    shopList: "",
    // num:number,
    cartTotal: "",
    isCheck: false,
    sum: 1,
    allSelect:true,
  },
  toOrder(){
    wx.navigateTo({
      url: '../purchase/purchase',
    })
  },
  bindManual: function(e) {
    console.log(e)
    let {
      productids: productId,
      id: id,
      sum: number,
      goodsid: goodsId
    } = e.currentTarget.dataset;
    var sum = e.detail.value;
    // // 将数值与状态写回  
    debugger
    wx.request({
      url: api.cart_update,
      method:"POST",
      header:{
        'X-Nideshop-Token': app.globalData.token
      },
      data:{
        id,
        goodsId: goodsid,
        number:sum,
        productId: productids,
      },
      success:res=>{
        console.log(res)
      }
    })
    // this.callback(productId, id, number, goodsId)
  },
  //购物为空跳到首页
  toIndexPage() {
    wx.switchTab({
      url: '../index/index',
    })
  },
  // 删除订单
  detailDeltab(e) {
    // console.log(e)
    let {
      productids
    } = e.currentTarget.dataset;
    let prod = productids.toString()
    console.log(prod)
    wx.request({
      url: api.shopdelete,
      method: "POST",
      header: {
        'X-Nideshop-Token': app.globalData.token
      },
      data: {
        productIds: prod
      },
      success:res=>{
        console.log(res)
        let shopList = res.data.data.cartList
        let cartTotal = res.data.data.cartTotal
        this.setData({
          shopList,
          cartTotal
        })
      }
    })
  },
  //回调函数
  callback(productId, id, number, goodsId) {
    wx.request({ //向后台请求重新渲染
      url: api.cart_update,
      method: 'POST',
      header: {
        'X-Nideshop-Token': app.globalData.token
      },
      data: { //从后台拿取的值
        id,
        goodsId,
        number,
        productId,
      },
      success: res => {
        console.log(res)
        let shopList = res.data.data.cartList //拿取更新值然后返给data
        let cartTotal = res.data.data.cartTotal
        this.setData({
          shopList,
          cartTotal
        })
      },
      fail(err) {
        console.log(err)
      }
    })
  },
  //减
  shopremove(e) {
    console.log(e)
    let {
      productids: productId,
      id: id,
      sum: number,
      goodsid: goodsId
    } = e.currentTarget.dataset;
    if (number > 1) {
      number--;
    }
    var minusStatus = number <= 1 ? 'disabled' : 'normal';
    this.callback(productId, id, number, goodsId)
  },
//跳回详情
  backToDetail(e) {
    console.log(e)
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: "../details/detail?id=" + id
    })
  },
  changeNumber(e) {
    let that = this
    let {
      productids: productId,
      id: id,
      sum: number,
      goodsid: goodsId
    } = e.currentTarget.dataset;
    let checkedGoodsAmount = e.currentTarget.dataset.checkedGoodsAmount
    wx.request({
      url: api.cart_update,
      method: 'POST',
      data: {
        id,
        goodsId,
        number: number,
        productId,
        checkedGoodsAmount: checkedGoodsAmount
      },
      header: {
        'X-Nideshop-Token': app.globalData.token
      },
      success(res) {
        console.log(res)
        let shopList = res.data.data.cartList
        let cartTotal = res.data.data.cartTotal
        that.setData({
          shopList,
          cartTotal
        })
      }
    })
  },

  //加
  shopadd(e) {
    let {
      productids: productId,
      id: id,
      sum: number,
      goodsid: goodsId
    } = e.currentTarget.dataset;
    let checkedGoodsAmount = e.currentTarget.dataset.checkedGoodsAmount
    number++;
    var minusStatus = number <= 1 ? 'disabled' : 'normal';
    this.callback(productId, id, number, goodsId)
  },
  //勾选
  check(e) {
    let that = this
    console.log(e)
    let {
      productids: productIds,
      ischecked: isChecked
    } = e.currentTarget.dataset
    //将产品ID和是否选择从后台拿过来弄成对象
    let index = e.currentTarget.dataset.index; // 获取data- 传进来的index
    let shopList = that.data.shopList; //重新获取购物车列表
    isChecked ? isChecked = 0 : isChecked = 1 // 改变isChecked的值
    //  console.log(productIds, isChecked)
    wx.request({ //向后台请求重新渲染
      url: api.cart_checked,
      method: 'POST',
      data: { //从后台拿取的值
        productIds,
        isChecked
      },
      header: {
        'X-Nideshop-Token': app.globalData.token
      },
      success(res) {
        console.log(res)
        let shopList = res.data.data.cartList //拿取更新值然后返给data
        let cartTotal = res.data.data.cartTotal
        that.setData({
          shopList,
          cartTotal
        })
      },
      fail(err) {
        console.log(err)
      }
    })
  },
  //全选
  checkall(e) {
    // let that = this
    let allSelect = this.data.allSelect
    allSelect = !allSelect
    console.log(e)
    let {product} = e.currentTarget.dataset;
    //将产品ID和是否选择从后台拿过来弄成对象
    // let shopList = that.data.shopList; //重新获取购物车列表
    var isChecked=[],
      productIds=[];
    product.forEach((el, index) => {
      productIds.push(el.product_id.toString) 
    })
    for (let i = 0; i < product.length; i++) {
      let checked = product[i].checked
      let productId = product[i].product_id
      checked ? checked = 0 : checked = 1 
      checked = allSelect;            
      isChecked.push(checked)
      productIds.push(productId)
    }
    wx.request({ //向后台请求重新渲染
      url: api.cart_checked,
      method: 'POST',
      data: { //从后台拿取的值
        productIds,
        isChecked
      },
      header: {
        'X-Nideshop-Token': app.globalData.token
      },
      success:res=> {
        console.log(res)
        let shopList = res.data.data.cartList 
        let cartTotal = res.data.data.cartTotal
        this.setData({
          shopList,
          cartTotal
        })
      }
    })
  },
  //加载阿贾克斯数据
  onShow: function() {
    let that = this
    wx.request({
      url: api.shopList,
      header: {
        'X-Nideshop-Token': app.globalData.token
      },
      success: function(res) {
        // console.log(res)
        let shopList = res.data.data.cartList
        // let num = res.data.data.cartList[0].number
        let cartTotal = res.data.data.cartTotal
        that.setData({
          shopList,
          // num,
          cartTotal
        })
      }
    })
  },
})